package com.finance.rule.service;


import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.rule.bean.pojo.SettlementRegionDetail;
import com.finance.rule.dao.SettlementRegionMapper;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.finance.rule.bean.pojo.SettlementRegion;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SettlementRegionService implements ISettlementRegionService {
	@Autowired
	private SettlementRegionMapper settlementRegionMapper;


	@Override
	public SettlementRegion getById(long id) {
		return settlementRegionMapper.getById(id);
	}

	@Override
	public SettlementRegionDetail getDetailById(long id) {
		return settlementRegionMapper.getDetailById(id);
	}

	@Override
	public List<SettlementRegion> listAll(){
		return settlementRegionMapper.listAll();
	}

	@Override
	public PageResultBO<SettlementRegion> listByPage(Page page) {
		PageResultBO<SettlementRegion>pageResultBO=new PageResultBO<SettlementRegion>();
		List<SettlementRegion>list=settlementRegionMapper.listByPage(page);
		pageResultBO.setTotalRecord(page.getTotalRecord());
		pageResultBO.setPageNo(page.getPageNo());
		pageResultBO.setPageSize(page.getPageSize());
		pageResultBO.setDataList(list);
		return pageResultBO;

	}

	@Override
	public PageResultBO<SettlementRegionDetail> listDetailByPage(Page page) {
		PageResultBO<SettlementRegionDetail>pageResultBO=new PageResultBO<SettlementRegionDetail>();
		List<SettlementRegionDetail>list=settlementRegionMapper.listDetailByPage(page);
		pageResultBO.setTotalRecord(page.getTotalRecord());
		pageResultBO.setPageNo(page.getPageNo());
		pageResultBO.setPageSize(page.getPageSize());
		pageResultBO.setDataList(list);
		return pageResultBO;
	}

	@Override
	public PageResultBO<Map<String,Object>> listDetailMapByPage(Page page){
		PageResultBO<Map<String,Object>>pageResultBO = new PageResultBO<Map<String,Object>>();
		List<Map<String,Object>>list = settlementRegionMapper.listDetailMapByPage(page);
		pageResultBO.setTotalRecord(page.getTotalRecord());
		pageResultBO.setPageNo(page.getPageNo());
		pageResultBO.setPageSize(page.getPageSize());
		pageResultBO.setDataList(list);
		return pageResultBO;
	}

	@Override
	public boolean add(SettlementRegion settlementRegion) {
		return settlementRegionMapper.add(settlementRegion) == 1;
	}

	@Override
	public boolean delete(String uId,List<String> ids) {
		return settlementRegionMapper.delete(uId,ids) > 0;
	}

	@Override
	public boolean update(SettlementRegion settlementRegion) {
		return settlementRegionMapper.update(settlementRegion) == 1;
	}

	@Transactional
	@Override
	public void updateSettlement(List< Map<String,String>> settlementLevelMap){
		if ( null != settlementLevelMap &&settlementLevelMap.size()>0){
			for ( Map<String,String> item :settlementLevelMap ){
				String settlementId = item.get("settlementId");
				String toCode = item.get("toCode");
				String settlementLevel = item.get("settlementLevel");
				if (StringUtils.isBlank(settlementId) && StringUtils.isNotBlank(settlementLevel)){
					settlementRegionMapper.addSettlement(Integer.parseInt(settlementLevel),Integer.parseInt(toCode));
				} else if (StringUtils.isNotBlank(settlementId) && StringUtils.isNotBlank(settlementLevel)){
					settlementRegionMapper.updateSettlement(Integer.parseInt(settlementLevel),Integer.parseInt(toCode));
				} else if (StringUtils.isNotBlank(settlementId) && StringUtils.isBlank(settlementLevel)){
					settlementRegionMapper.deleteSettlement(Integer.parseInt(toCode));
				}
			}
		}
	}

	@Override
	public String getIdByName(String name) {
		return settlementRegionMapper.getIdByName(name);
	}

	@Override
	public List<String> getIds(String level, String code) {
		return settlementRegionMapper.getIds(level,code);
	}
}
